package dataImport.action.manager.storage;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import dataImport.action.abstracts.AbstractEntityReader;
import dataImport.action.manager.EntityReaderManager;
import dataImport.exception.MissingReader;


public class EntityReaderStorage implements Serializable {

	private static final long serialVersionUID = -2634355851975255738L;

	private HashMap<EntityReaderStorageKey, AbstractEntityReader> readers;

	public EntityReaderStorage() {
		this.readers = new HashMap<EntityReaderStorageKey, AbstractEntityReader>();
	}

	public void add(final EntityReaderStorageKey entityReaderStorageKey) throws MissingReader {
		this.readers.put(entityReaderStorageKey, EntityReaderManager.createReader(entityReaderStorageKey));
	}

	public void add(final EntityReaderStorageKey... entityReaderStorageKeys) throws MissingReader {
		for (EntityReaderStorageKey entityReaderStorageKey : entityReaderStorageKeys) {
			this.add(entityReaderStorageKey);
		}
	}

	public void addAll() throws MissingReader {
		this.add(EntityReaderStorageKey.values());
	}

	public AbstractEntityReader get(final EntityReaderStorageKey entityReaderStorageKey) throws MissingReader {
		if (this.contains(entityReaderStorageKey)) {
			return this.readers.get(entityReaderStorageKey);
		}

		throw new MissingReader(entityReaderStorageKey);
	}

	public Collection<AbstractEntityReader> getAll() {
		return this.readers.values();
	}

	public boolean contains(final EntityReaderStorageKey entityReaderStorageKey) throws MissingReader {
		if (!this.readers.containsKey(entityReaderStorageKey)) {
			throw new MissingReader(entityReaderStorageKey);
		}

		return true;
	}
}
